# 二分法求[a, b]间的实根, 误差在error之内, 函数为y = f(x)
def do_calculate(a, b, error, f):
    assert b > a
    while b - a >= error:
        mid = (b + a) / 2.0
        if f(mid) == 0:
            return mid
        elif f(mid) > 0:
            if f(b) > f(a):
                b = mid
            else:
                a = mid
        else:
            if f(b) > f(a):
                a = mid
            else:
                b = mid
    return (b + a) / 2.0
